﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Publishers : System.Web.UI.Page
{
    FileUpload _fileUpload;
    Image _imgUpload;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[EClass.ESession.Admin.ToString()] == null)
        {
            Response.Redirect("Login.aspx", true);
        }
    }
    protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
    {
        Title = e.CommandName + "?" + e.CommandArgument;
        if (e.CommandName.Equals("Upload"))
        {
            bool up = false;
            try
            {
                DetailsView view = sender as DetailsView;

                for (int i = 0; (i < view.Rows.Count && !up); i++)
                {
                    DetailsViewRow row = view.Rows[i];
                    if (row.RowType == DataControlRowType.DataRow)
                    {

                        _fileUpload = row.FindControl("FileUpload2") as FileUpload;
                        _imgUpload = row.FindControl("Image3") as Image;
                        if (_imgUpload == null || _fileUpload == null)
                            continue;

                        if (_fileUpload.HasFile)
                        {
                            _fileUpload.SaveAs(Server.MapPath("~/Images/") + _fileUpload.FileName);
                            _imgUpload.ImageUrl = "~/Images/" + _fileUpload.FileName;
                            up = true;
                        }
                    }
                }
            }
            catch
                (Exception ex)
            {
                Response.Write("<script> alert '" + ex.ToString() + "' </script>");
            }
        }
        if(e.CommandName == "Cancel")
        {
            GridView1.DataSourceID = _sdsPublishers.ID;
            GridView1.DataBind();
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Title = e.CommandName + "?" + e.CommandArgument;
        if (e.CommandName == "New")
        {
            GridView1.DataSourceID = "";
            GridView1.DataBind();
        }
        if (e.CommandName.Equals("Upload"))
        {
            bool up = false;
            for (int i = 0; (i < GridView1.Rows.Count && !up); i++)
            {
                GridViewRow row = GridView1.Rows[i];
                if (row.RowType == DataControlRowType.DataRow)
                {
                    try
                    {
                        _fileUpload = row.FindControl("FileUpload1") as FileUpload;
                        _imgUpload = row.FindControl("Image2") as Image;
                        
                        if (_imgUpload == null || _fileUpload == null)
                            continue;

                        if (_fileUpload.HasFile)
                        {
                            _fileUpload.SaveAs(Server.MapPath("~/Images/") + _fileUpload.FileName);
                            _imgUpload.ImageUrl = "~/Images/" + _fileUpload.FileName;
                            _imgUpload.AlternateText = "~/Images/" + _fileUpload.FileName;
                            up = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        Response.Write("<script> alert '" + ex.ToString() + "' </script>");
                    }
                }
            }
        }

    }
}
